package edu.mju.serviceacl.controller;


import edu.mju.commonutils.ReturnCode;
import edu.mju.serviceacl.entity.Permission;
import edu.mju.serviceacl.service.PermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 */
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    /**
     * 获取全部菜单
     * @return json格式
     */
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public ReturnCode indexAllPermission() {
        List<Permission> list =  permissionService.queryAllMenu();
        Map<String,Object> map = new HashMap<>();
        map.put("children",list);
        return ReturnCode.success().data(map);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public ReturnCode remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return ReturnCode.success();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public ReturnCode doAssign(String roleId,String[] permissionId) {
        permissionService.saveRolePermission(roleId,permissionId);
        return ReturnCode.success();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public ReturnCode toAssign(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectAllMenu(roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("children", list);
        return ReturnCode.success().data(map);
    }



    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public ReturnCode save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return ReturnCode.success();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public ReturnCode updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return ReturnCode.success();
    }

}

